/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#include "ResourceMessages.hpp"

namespace XernProtocol
{
	// ResourceRequest
	ResourceRequest::ResourceRequest()
	{
	}

	ResourceRequest::~ResourceRequest()
	{
	}

	MessageType ResourceRequest::GetType() const
	{
		return MT_RESOURCE_REQUEST;
	}

	size_t ResourceRequest::GetSize()
	{
		return Size;
	}

	void ResourceRequest::Decode(DataStream &in, size_t size)
	{
		in >> resourceId;
	}

	void ResourceRequest::Encode(DataStream &out) const
	{
		out << resourceId;
	}

	// ResourceRequestData.
	ResourceRequestData::ResourceRequestData()
	{
	}

	ResourceRequestData::~ResourceRequestData()
	{
	}

	MessageType ResourceRequestData::GetType() const
	{
		return MT_RESOURCE_REQUEST_DATA;
	}

	size_t ResourceRequestData::GetSize()
	{
		return Size;
	}

	void ResourceRequestData::Decode(DataStream &in, size_t size)
	{
		in >> resourceId;
	}

	void ResourceRequestData::Encode(DataStream &out) const
	{
		out << resourceId;
	}

	// ResourceData
	ResourceData::ResourceData()
	{
		blockid = 0;
		blocksize = 0;
	}

	ResourceData::~ResourceData()
	{
	}

	MessageType ResourceData::GetType() const
	{
		return MT_RESOURCE_DATA;
	}

	size_t ResourceData::GetSize()
	{
		return BaseSize + blocksize;
	}

	void ResourceData::Decode(DataStream &in, size_t size)
	{
		in >> resourceId >> blockid >> blocksize;
		in.Read(blocksize, block.c_array());
	}

	void ResourceData::Encode(DataStream &out) const
	{
		out << resourceId << blockid << blocksize;
		out.Write(blocksize, const_cast<ResourceData*> (this)->block.c_array());
	}

	// ResourceDescription
	ResourceDescription::ResourceDescription()
	{
	}

	ResourceDescription::~ResourceDescription()
	{
	}

	MessageType ResourceDescription::GetType() const
	{
		return MT_RESOURCE_DESCRIPTION;
	}

	size_t ResourceDescription::GetSize()
	{
		return BaseSize + resourceMime.size() + resourceExtension.size();
	}

	void ResourceDescription::Decode(DataStream &in, size_t size)
	{
		in >> resourceId >> resourceHash >> resourceFlags >> resourceSize;
		in >> numblocks >> blocksize >> resourceMime >> resourceExtension;
	}

	void ResourceDescription::Encode(DataStream &out) const
	{
		out << resourceId << resourceHash << resourceFlags << resourceSize;
		out << numblocks << blocksize << resourceMime << resourceExtension;
	}

	// ResourceDenial
	ResourceDenial::ResourceDenial()
	{
	}

	ResourceDenial::~ResourceDenial()
	{
	}

	MessageType ResourceDenial::GetType() const
	{
		return MT_RESOURCE_DENIAL;
	}

	size_t ResourceDenial::GetSize()
	{
		return BaseSize + reason.size();
	}

	void ResourceDenial::Decode(DataStream &in, size_t size)
	{
		in >> resourceId >> code >> reason;
	}

	void ResourceDenial::Encode(DataStream &out) const
	{
		out << resourceId << code << reason;
	}

	// ResourceState
	ResourceState::ResourceState()
	{
		statesize = 0;
	}

	ResourceState::~ResourceState()
	{
	}

	MessageType ResourceState::GetType() const
	{
		return MT_RESOURCE_STATE;
	}

	size_t ResourceState::GetSize()
	{
		return BaseSize + statesize;
	}

	void ResourceState::Decode(DataStream &in, size_t size)
	{
		in >> resourceId >> statesize;
		in.Read(statesize, state.c_array());
	}

	void ResourceState::Encode(DataStream &out) const
	{
		out << resourceId << statesize;
		out.Write(statesize, const_cast<ResourceState*> (this)->state.c_array());
	}
}
